__usage__='''call examples:
python run_tg.py -quite -problem=problemName -p=degree -N=res ... supress colors and progress information.
python run_tg.py -problem=problemName -p=degree -N=res ... colors and progress information.

problemName can be'rs' for rotated square,'ds' for driven square, 'zd' for Zalesak's disk, 'all' for all the prob-
lems.
degree is int, polynomial degreee of approx. base functions.
res is int, spatial resolution for 'ds' domain has [2*N,N] squares, 'ds' and 'zs' have [2*N,2*N]
'''

from problems.aux import get_problem
from solvers.tg import *
from graphics.lineplot import LinePlot
from graphics.colorprint import ColorPrint,colors
import time,sys,random

if __name__ == '__main__':
    if not(len(sys.argv) == 4 or len(sys.argv) == 5):
        print __usage__
        sys.exit()
    elif len(sys.argv) == 4:
        if not('-problem=' in sys.argv[1][:9] and '-p=' in sys.argv[2][:3] and '-N=' in sys.argv[3][:3]):
            print __usage__
            sys.exit()
        else:
            sColor = 'red'
            iColor = random.choice(colors.keys())
            pgb = True
            problemName = sys.argv[1][9:]
            p = int(sys.argv[2][3:])
            N = int(sys.argv[3][3:])
    else: 
        if not(sys.argv[1] == '-quite' and '-problem=' in sys.argv[2][:9] and '-p=' in sys.argv[3][:3] and '-N=' in sys.argv[4][:3]):
            print __usage__
            sys.exit()
        else:
            sColor = 'normal'
            iColor = 'normal'
            pgb = False
            problemName = sys.argv[2][9:]
            p = int(sys.argv[3][3:])
            N = int(sys.argv[4][3:])

    options={}
    options ['N'] = N           # spatial resolution
    options['p'] = p            # polynomial degree
    options['results-path'] = './results/results-taylor-galerkin-N=%d-p=%d' % (options['N'],options['p']) # path to results 

    lp = LinePlot()
    cp = ColorPrint(colors)

    # p=1 should come from outside, for type==1 use 2
    # test all but lw2
    for type in [0,1,2,3]:
        p_degree = options['p'] if type != 1 else 2
        for bcStrong in [0,1] if type != 0 else [0]:
            
            slwf = lw_fem.LaxWendroffFEM(p_degree,type,bcStrong)            # types 0,1,2,3;  !
            slwtg = lw_tg.LaxWendroffTG(p_degree,type,bcStrong)             # types 0,1,2,3;  ! 
            ssel = selmin.Selmin(p_degree,type,bcStrong)                    # types 0,1,2,3;  ! 
            sttg4a = ttg4a.TTG4A(p_degree,type,bcStrong)                    # types 0,1,2,3;  !
            sttg4b = ttg4b.TTG4B(p_degree,type,bcStrong)                    # types 0,1,2,3;  !
            scn = cn.CrankNicolsonTG(p_degree,type,bcStrong)                # types 0,1,2,3;  !
    
            for solver in [slwf,slwtg,ssel,sttg4a,sttg4b,scn]:
                solver.pgb = pgb
                for problem, sf in get_problem(problemName,options):
                    solver.sf = sf
                    
                    print '%s' % ('-'*70)
                    print cp('Solving %s problem using %s %s method:' % (problem.problemDir,solver.methodDir,solver.solverDir),iColor)
                    start = time.time()
                    solver.solve(problem)
                    lp.plot_all(problem,solver)
                    print cp('\nDone(CFL=%g) in %s%s' % (solver.CFL,cp('%g s' % (time.time()-start),sColor),cp('!',iColor)),iColor)
                    print '%s' % ('-'*70)
                    
                    del problem
                del solver
    
    # test lw2
    for type in [0,1]:
        p_degree = options['p'] if type != 1 else 2
        for bcStrong in [0,1] if type != 0 else [0]:
            
            slw2 = lw_2.LaxWendroff2(p_degree,type,bcStrong)                # types 0,1;      !
            
            for solver in [slw2]:
                solver.pgb = pgb
                for problem, sf in get_problem(problemName,options):
                    solver.sf = sf
                    
                    print '%s' % ('-'*70)
                    print cp('Solving %s problem using %s %s method:' % (problem.problemDir,solver.methodDir,solver.solverDir),iColor)
                    start = time.time()
                    solver.solve(problem)
                    lp.plot_all(problem,solver)
                    print cp('\nDone(CFL=%g) in %s%s' % (solver.CFL,cp('%g s' % (time.time()-start),sColor),cp('!',iColor)),iColor)
                    print '%s' % ('-'*70)
                    
                    del problem
                del solver


